package com.imooc.config;

import com.imooc.interceptor.UserTokenInterceptor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName: WebMvcConfig
 * @Package: com.imooc.config
 * @Description:
 * @Date: 2021/7/20 15:40
 * @Author: sunzheng@beiming.com
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

  @Bean
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.build();
  }

  // 实现静态资源的映射
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
        .addResourceLocations("classpath:/META-INF/resources/") // 映射swagger2
        .addResourceLocations("file:D:\\CODE\\projects\\images\\"); // 映射本地静态资源
  }

  @Bean
  public UserTokenInterceptor userTokenInterceptor() {
    return new UserTokenInterceptor();
  }

  /**
   * 添加用户 token 拦截器
   *
   * @param registry 注册表
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {

    registry.addInterceptor(userTokenInterceptor())
        .addPathPatterns("/hello")
        .addPathPatterns("/shopcart/add")
        .addPathPatterns("/shopcart/del")
        .addPathPatterns("/address/list")
        .addPathPatterns("/address/add")
        .addPathPatterns("/address/update")
        .addPathPatterns("/address/setDefalut")
        .addPathPatterns("/address/delete")
        .addPathPatterns("/orders/*")
        .addPathPatterns("/center/*")
        .addPathPatterns("/userInfo/*")
        .addPathPatterns("/myorders/*")
        .addPathPatterns("/mycomments/*")
        .excludePathPatterns("/myorders/deliver")
        .excludePathPatterns("/orders/notifyMerchantOrderPaid");

    WebMvcConfigurer.super.addInterceptors(registry);
  }
}
